package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.NutritionProductMapper;
import ym.pojo.NutritionProduct;

import java.util.List;

@DubboService
@Transactional
public class NutritionProductServiceImpl implements NutritionProductService {
    @Autowired
    private NutritionProductMapper nutritionProductMapper;

    @Override
    public void add(NutritionProduct nutritionProduct) {
        nutritionProductMapper.insert(nutritionProduct);
    }

    @Override
    public void update(NutritionProduct nutritionProduct) {
        nutritionProductMapper.updateById(nutritionProduct);
    }

    @Override
    public void delete(Integer id) {
        nutritionProductMapper.deleteById(id);
    }

    @Override
    public NutritionProduct findById(Integer id) {
        return nutritionProductMapper.selectById(id);
    }

    @Override
    public List<NutritionProduct> findAll() {
        return nutritionProductMapper.selectList(null);
    }

    @Override
    public Page<NutritionProduct> search(String search, int page, int size) {
        QueryWrapper<NutritionProduct> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("name", search);
        }
        return nutritionProductMapper.selectPage(new Page(page, size), queryWrapper);
    }
}
